package tw.asap.plus.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.User;
import tw.asap.plus.ui.activity.AboutActivity;
import tw.asap.plus.ui.activity.AddressListActivity;
import tw.asap.plus.ui.activity.CollectionActivity;
import tw.asap.plus.ui.activity.CouponActivity;
import tw.asap.plus.ui.activity.LoginAsapActivity;
import tw.asap.plus.ui.activity.MessageActivity;
import tw.asap.plus.ui.custom.AppBar;
import tw.asap.plus.ui.custom.ImageTextButton;
import tw.asap.plus.ui.custom.MineItemView;
import tw.asap.plus.utils.Toaster;

/**
 * Created by Yax on 15/11/1.
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    @InjectView(R.id.appbar)
    AppBar appbar;
    @InjectView(R.id.img_user)
    SimpleDraweeView imgUser;
    @InjectView(R.id.tv_username)
    TextView tvUsername;
    @InjectView(R.id.itb_my_collection)
    ImageTextButton itbMyCollection;
    @InjectView(R.id.itb_my_coupon)
    ImageTextButton itbMyCoupon;
    @InjectView(R.id.itb_my_message)
    ImageTextButton itbMyMessage;
    @InjectView(R.id.menu_my_address)
    MineItemView menuMyAddress;
    @InjectView(R.id.menu_share)
    MineItemView menuShare;
    @InjectView(R.id.menu_help)
    MineItemView menuHelp;
    @InjectView(R.id.menu_callback)
    MineItemView menuCallback;
    private View view;

    public static Fragment newInstance() {
        return new MineFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_mine, null);
        } else {
            if (view.getParent() != null)
                ((ViewGroup) view.getParent()).removeView(view);
        }
        ButterKnife.inject(this, view);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        imgUser.setOnClickListener(this);
        tvUsername.setOnClickListener(this);
        itbMyCollection.setOnClickListener(this);
        itbMyCoupon.setOnClickListener(this);
        itbMyMessage.setOnClickListener(this);
        menuMyAddress.setOnClickListener(this);
        menuShare.setOnClickListener(this);
        menuHelp.setOnClickListener(this);
        menuCallback.setOnClickListener(this);
        appbar.setRightNavigation(R.drawable.icon_setting, v -> {
            startActivity(new Intent(getActivity(), AboutActivity.class));
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (User.hasLogin()) {
            tvUsername.setText(User.getUserName());
        }else{
            tvUsername.setText(getString(R.string.no_login));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    public boolean checkLoginState() {
        if (!User.hasLogin()) {
            Toaster.s(getActivity(), getString(R.string.user_not_login));
            startActivity(new Intent(getActivity(), LoginAsapActivity.class));
            return false;
        }

        return true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_username:
            case R.id.img_user:
                checkLoginState();
                break;
            case R.id.itb_my_collection:
                if (checkLoginState()) {
                    startActivity(new Intent(getActivity(), CollectionActivity.class));
                }
                break;
            case R.id.itb_my_coupon:
                if (checkLoginState()) {
                    startActivity(new Intent(getActivity(), CouponActivity.class));
                }
                break;
            case R.id.itb_my_message:
                if (checkLoginState()) {
                    startActivity(new Intent(getActivity(), MessageActivity.class));
                }
                break;
            case R.id.menu_my_address:
                if (checkLoginState()) {
                    Intent intent = new Intent(getActivity(), AddressListActivity.class);
                    intent.putExtra("FROM_MINE",true);
                    startActivity(intent);
                }
                break;

        }
    }
}
